package com.hzqy.service.zxbh;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.service.zxbh.utils.HttpUtil;
import com.hzqy.web.vo.SysConfigVo;
import com.hzqy.web.vo.zxbh.GanKaoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Service
public class ZxbhService {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());

    //获取v2直播
    public Map<String, String> sendRequestToServer(String token, HttpServletRequest request, GanKaoVo vo) {
        Map<String, String> resMap = new HashMap<>();
        String req_url = vo.getJumpurl();
        LOG.debug("req_url:" + req_url + ";token:" + token);
        if (req_url.startsWith("http:")) {
            Map<String, String> sysConfigkv = this.findSysConfigByKey("ZhiboV2_zhilianxue_ThirdAdmin");
            String CLIENT_ID = sysConfigkv.get("CLIENT_ID");
            String CLIENT_SECRET = sysConfigkv.get("CLIENT_SECRET");
            Map<String, String> heards = new HashMap<String, String>();
            //heards.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes()));
            heards.put("Authorization", "Bearer " + token);
            heards.put("access-token", token);
            resMap = HttpUtil.get(req_url, heards);
        } else {
            return null;
        }
        LOG.debug("resMap:" + resMap);
        return resMap;
    }


    //获取配置
    public Map<String, String> findSysConfigByKey(String key) {
        Map<String, String> resMap = new HashMap<>();
        SysConfigVo sysConfigVo = new SysConfigVo();
        sysConfigVo.setF_sc_key(key);
        String content = SystemConfigUtils.getSysConfig(key);
        String content_s[] = content.split(",");
        for (int i = 0; i < content_s.length; i++) {
            String s = content_s[i];
            String[] sing = s.split(":");
            resMap.put(sing[0], sing[1]);
        }
        resMap.put("code", "200");
        return resMap;
    }

    /**
     * 获取v2直播token
     *
     * @param request
     * @param vo
     * @return
     */
    public Map<String, String> sendRequestGetToken(HttpServletRequest request, GanKaoVo vo) {
        Map<String, String> resMap = new HashMap<>();
        String req_url = "";
        try {
            req_url = vo.getJumpurl();
            if (req_url == null || "null".equals(req_url) || "".equals(req_url)) {
                req_url = SystemConfigUtils.getSysConfig("zlx_auth_token_url"); // 获取token接口
            }
        } catch (Exception e) {
            if (req_url == null || "null".equals(req_url) || "".equals(req_url)) {
                req_url = SystemConfigUtils.getSysConfig("zlx_auth_token_url"); // 获取token接口
            }
        }
        Map<String, String> sysConfigkv = this.findSysConfigByKey("ZhiboV2_zhilianxue_ThirdAdmin");
        if (!"200".equals(sysConfigkv.get("code"))) {
            return sysConfigkv;
        }
        String CLIENT_ID = sysConfigkv.get("CLIENT_ID");
        String CLIENT_SECRET = sysConfigkv.get("CLIENT_SECRET");
        Map<String, String> heards = new HashMap<String, String>();
        heards.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes()));
        if (req_url.startsWith("http:")) {
            resMap = HttpUtil.sendPost(req_url, sysConfigkv, heards);
        } else {
            return null;
        }
        LOG.debug("resMap:" + resMap);
        return resMap;
    }
}
